﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLACQUY.BUS;
using System.Collections;
using QLACQUY.DTO;

namespace QLACQUY
{
    public partial class QuanLyPhieuGiaoHang : Form
    {
        public QuanLyPhieuGiaoHang()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ThemPhieuGiaoHang frm = new ThemPhieuGiaoHang();
            frm.ShowDialog();
            fetchDataToGrid();
        }

        private void QuanLyPhieuGiaoHang_Load(object sender, EventArgs e)
        {
            fetchDataToGrid();

        }
        private void fetchDataToGrid()
        {
            dataGridView1.Rows.Clear();
            PhieuGiaoHangBUS bus = new PhieuGiaoHangBUS();
            ArrayList arr = bus.layDanhSach();
            for (int i = 0; i < arr.Count; i++)
            {
                int k = dataGridView1.Rows.Add();
                if (k == 0)
                    dataGridView1.Rows[k].Cells["STT"].Value = 1;
                else
                    dataGridView1.Rows[k].Cells["STT"].Value = int.Parse(dataGridView1.Rows[k - 1].Cells["STT"].Value.ToString()) + 1;
                
                dataGridView1.Rows[k].Cells["MaPhieu"].Value = ((PhieuGiaoHangCRT)arr[i]).MaPhieu;
                dataGridView1.Rows[k].Cells["NgayLapPhieu"].Value = ((PhieuGiaoHangCRT)arr[i]).NgayLapPhieu;
                dataGridView1.Rows[k].Cells["TenKH"].Value = ((PhieuGiaoHangCRT)arr[i]).TenKhachHang;
                dataGridView1.Rows[k].Cells["DienThoai"].Value = ((PhieuGiaoHangCRT)arr[i]).DienThoai;
                dataGridView1.Rows[k].Cells["DiaChi"].Value = ((PhieuGiaoHangCRT)arr[i]).DiaChi;
                dataGridView1.Rows[k].Cells["TongTien"].Value = ((PhieuGiaoHangCRT)arr[i]).TongTien;
                dataGridView1.Rows[k].Cells["MaKH"].Value = ((PhieuGiaoHangCRT)arr[i]).MaKH;
                dataGridView1.Rows[k].Cells["NoCu"].Value = ((PhieuGiaoHangCRT)arr[i]).NoCu;
                dataGridView1.Rows[k].Cells["TienKhachTra"].Value = ((PhieuGiaoHangCRT)arr[i]).TienKhachTra;


            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            fetchDataToGrid();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
                return;
            
            int makh = (int)dataGridView1.SelectedRows[0].Cells["MaKH"].Value;
            int maphieu = (int)dataGridView1.SelectedRows[0].Cells["MaPhieu"].Value;
            double tongtien = (double)dataGridView1.SelectedRows[0].Cells["TongTien"].Value;
            double nocu = (double)dataGridView1.SelectedRows[0].Cells["NoCu"].Value;
            double tienkhachtra = (double)dataGridView1.SelectedRows[0].Cells["TienKhachTra"].Value;
            double tongno = tongtien+nocu-tienkhachtra;
            if(tongno < 0) tongno = 0;
            string tienchu = Global.DocTienBangChu((long)(tongno), " đồng");
            BaoCao2 frm = new BaoCao2(tienchu, makh, maphieu);
            frm.Show();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
                return;
            if(MessageBox.Show("Chắc chắn xóa?","Xac nhan", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                PhieuGiaoHangBUS bus = new PhieuGiaoHangBUS();
                for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
                {
                    int maphieu = (int)dataGridView1.SelectedRows[i].Cells["MaPhieu"].Value;
                    bus.xoaPhieuGiaoHang(maphieu);
                }
                MessageBox.Show("Xóa thành công!");
                fetchDataToGrid();
            }
            
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count <= 0)
                return;
            CapNhatPhieuGiaoHang frm = new CapNhatPhieuGiaoHang();
            frm.MaKH = (int)dataGridView1.SelectedRows[0].Cells["MaKH"].Value;
            frm.ShowDialog();
            fetchDataToGrid();
        }
    }
}
